﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace BPP
{
    public partial class ShowSelectedPath : Form
    {
        public ShowSelectedPath()
        {
            InitializeComponent();
            cmbPathsToShow.Items.Add("cpd:C00116 -> rn:R00240 -> cpd:C00184");
            cmbPathsToShow.SelectedIndex = 0;
        }




        //_programm.compoundInformation compounfInformation;

        _programm.Metabolites compounfInformation;


        private void btnShowPathInfo_Click(object sender, EventArgs e)
        {

            loadGridViewData();

        }

        public void loadGridViewData()
        {
            listOfElement.Clear();
            gwShowPathElement.Rows.Clear();
            gwShowPathElement.Columns.Clear();

            //vzjatj strukturu dokumenta kakogo to
            //todo reaction fields
            string[] fields = new string[13] {  "link", 
                                                "name",
                                                "formula(neutral)", 
                                                "formula(charged)", 
                                                "charge", 
                                                "compartment", 
                                                "keggid", 
                                                "chebid", 
                                                "pubchemid", 
                                                "inchi", 
                                                "smiles", 
                                                "abbeviation", 
                                                 "" };

            gwShowPathElement.Columns.Add("", "");
            for (int i = 0; i < 13; i++)
            {
                gwShowPathElement.Rows.Add(new DataGridViewRow());
                gwShowPathElement.Rows[i].Cells[0].Value = fields[i];
            }


            //select path to analyze
            string path = cmbPathsToShow.SelectedItem.ToString();


            string[] path_element = path.Split(new string[1] { " -> " }, StringSplitOptions.RemoveEmptyEntries);



            for (int i = 0; i < path_element.Length; i++)
            {
                int column_index = gwShowPathElement.Columns.Add(path_element[i], path_element[i]);

                if (path_element[i].StartsWith("cpd"))
                {
                    //read data from database and fill gridview
                    compounfInformation = new _programm.Metabolites(); // _programm.compoundInformation();


                    bool readed = compounfInformation.FillCompoundDataToGridView(path_element[i].Substring(4));


                    if (!readed)
                    {
                        _programm.keggObject _kegg = _programm.shared_functions.GetKeggDataByCompound(path_element[i].Substring(4));

                        compounfInformation.Link = _kegg.link;
                        compounfInformation.Name = _kegg.Name;
                        compounfInformation.Formula_charged = _kegg.formula[0];
                        compounfInformation.Formula_neutral = "";
                        compounfInformation.Charge = _kegg.charge;
                        compounfInformation.Compartment = "";
                        compounfInformation.KEGG_ID = _kegg.keggID;
                        compounfInformation.ChEBI_ID = _kegg.chebiID;
                        compounfInformation.PubChem_ID = _kegg.pubchemID;
                        compounfInformation.InChI = "";
                        compounfInformation.SMILES = "";
                        compounfInformation.Abbreviation = "";
                        
                    }


                    gwShowPathElement.Rows[0].Cells[column_index].Value = compounfInformation.Link ;
                    gwShowPathElement.Rows[1].Cells[column_index].Value = compounfInformation.Name;
                    gwShowPathElement.Rows[2].Cells[column_index].Value = compounfInformation.Formula_charged;
                    gwShowPathElement.Rows[3].Cells[column_index].Value = compounfInformation.Formula_neutral;
                    gwShowPathElement.Rows[4].Cells[column_index].Value = compounfInformation.Charge;
                    gwShowPathElement.Rows[5].Cells[column_index].Value = compounfInformation.Compartment;
                    gwShowPathElement.Rows[6].Cells[column_index].Value = compounfInformation.KEGG_ID;
                    gwShowPathElement.Rows[7].Cells[column_index].Value = compounfInformation.ChEBI_ID;
                    gwShowPathElement.Rows[8].Cells[column_index].Value = compounfInformation.PubChem_ID;
                    gwShowPathElement.Rows[9].Cells[column_index].Value = compounfInformation.InChI;
                    gwShowPathElement.Rows[10].Cells[column_index].Value = compounfInformation.SMILES;
                    gwShowPathElement.Rows[11].Cells[column_index].Value = compounfInformation.Abbreviation;
                    


                    listOfElement.Add(compounfInformation);
                }
                if (path_element[i].StartsWith("rn"))
                {

                }

                gwShowPathElement.Rows[gwShowPathElement.Rows.Count - 1].Cells[i + 1] = new DataGridViewButtonCell();
                gwShowPathElement.Rows[gwShowPathElement.Rows.Count - 1].Cells[i + 1].Value = "SHOW DATA";


            }

            gwShowPathElement.Refresh();
        }

        List<_programm.Metabolites> listOfElement = new List<_programm.Metabolites>();

        private void gwShowPathElement_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (((DataGridView)sender).CurrentCell.Value.ToString() == "SHOW DATA")
            {
                //peredajotaa infa iz poslednego scitannoogo
                //sdealtj list c compoundami iz bazi dannih
                //i podavatj toljko tot, kotorij nuzen
                CompoundData f = new CompoundData(((DataGridView)sender).CurrentCell.OwningColumn.HeaderText,
                    listOfElement[((DataGridView)sender).CurrentCell.OwningColumn.Index/2], this);

                f.Show();
              
            }
        }

       
    }

    
}
